﻿@model IEnumerable<CmsAdminWorkflowActiveModel>

@functions 
{
    string DisplayNullableDates(DateTime? date)
    {
        return date.HasValue ?
            date.Value.ToString("dd/MM/yyyy") :
            "N/A";
    }
    string DisplayAssignedUser(CmsAdminWorkflowActiveModel model)
    {
        string result = string.Empty;

        if(model.UserId.HasValue)
        {
            result = model.User;
        }
        else if(model.UserGroupId.HasValue)
        {
            result = model.UserGroup;
        }
        
        return result;
    }
}

@(Html.CmsGrid<CmsAdminWorkflowActiveModel>(Model, new { @class = "t-grid" }).Wrapper("ActiveItemGrid")
    .Columns(
            columns =>
            {
                columns.HeaderCssClass("t-grid-header").Bound().Width(40).Title("Content title").HtmlAttributes(new { @class = "t-header" });
                columns.Bound().Title("Type").HtmlAttributes(new { @class = "t-header" });                
                columns.Bound().Title("Cycle").HtmlAttributes(new { @class = "t-header" });
                columns.Bound().Title("Status").HtmlAttributes(new { @class = "t-header" });
                columns.Bound().Title("Assigned to").HtmlAttributes(new { @class = "t-header" });
                columns.Bound().Title("Approval date").HtmlAttributes(new { @class = "t-header" });
                columns.Bound().Title("").HtmlAttributes(new { @class = "t-header" });
                columns.Bound().Width(40).Title("").HtmlAttributes(new { @class = "t-header" });
                columns.Bound().Width(40).Title("").HtmlAttributes(new { @class = "t-header" });
            })
    .Rows(
        rows =>
        {
            rows.Bound().RowItemTemplate(
                @<text>
                <a href="@Url.Action("workflowitem", "cmspage", new { id = item.Id })">@item.Title</a>
                </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@item.EntityType</span>
                </text>
                ).HtmlAttributes(new { @class = "text-center" });
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@string.Format("{0}.{1}", item.Cycle, item.State)</span>
                </text>
                ).HtmlAttributes(new { @class = "text-center" });
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@item.Status</span>
                </text>
                ).HtmlAttributes(new { @class = "text-center" });
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@DisplayAssignedUser(item)</span>
                </text>
                ).HtmlAttributes(new { @class = "text-center" });
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@DisplayNullableDates(item.ApprovalDate)</span>
                </text>
                ).HtmlAttributes(new { @class = "text-center" });
            rows.Bound().RowItemTemplate(
                @<text>
                <span>@item.Id</span>
                </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>
                     <a class="b-icon b-icon_delete" title="Page" href="@Url.Action("delete", "cmspage", new {id = item.Id})">Delete</a>
                 </text>
                );
            rows.Bound().RowItemTemplate(
                @<text>
                     <input type="checkbox" name="itemselected" data-entitytype="@item.EntityType" value="@item.Id"/>
                </text>
                ).HtmlAttributes(new { @class = "t-last" }); 
        })
        .Render()
        )
      
@if (Model.Any())
{
    Html.RenderPartial("MassActionsPanel",
                       new MassActionsPanelModel()
                           {
                               GridId = "ActiveItemGrid",
                               Actions = new[]
                                             {
                                                 new MassActionModel() {Title = "Withdraw", JsHandler = "massActions.Withdraw"},
                                                 new MassActionModel() {Title = "Delete", JsHandler = "massActions.Delete"}
                                             }
                           }
        );
}